var fs = require('fs');
var dim = require('./dimension.js');
var reader = require('./reader.js');
var record = require('./record.js');
var dept = require('./dept.js');
var Chord = require('./chord.js');

fs.readFile('source.txt','utf-8',function(e,data){
	var rowDatas = data.split('\n');
	var dimension = new dim.Dimension(),
		records = new record(),
		depts = new dept(),
		chord,
		row,
		chart,
		files = [],
		idx,
		path = '',
		key;

	for( idx=0;idx<rowDatas.length-1;idx++){
		row = rowDatas[idx];
		dimension.add(row,reader.dimension);
		records.add(row,reader.record);
	}
	dimension.show();
	records.filter();
	
	for( key in records.values ){
		depts.add(records.values[key],reader.dept);
	}

	//depts.show();


	depts.groupDepts();
	depts.groupDept();

	depts.showGroupedDepts(); 
	depts.showGroupedDept();

	files.push({file: '201601',start: '1-1-2016',end: '1-31-2016'});	
	files.push({file: '201602',start: '2-1-2016',end: '2-29-2016'});	
	files.push({file: '201603',start: '3-1-2016',end: '3-31-2016'});	
	files.push({file: '201604',start: '4-1-2016',end: '4-30-2016'});	
	files.push({file: '201605',start: '5-1-2016',end: '5-31-2016'});	
	files.push({file: '201606',start: '6-1-2016',end: '6-30-2016'});	
	files.push({file: '201607',start: '7-1-2016',end: '7-31-2016'});	
	files.push({file: '201608',start: '8-1-2016',end: '8-30-2016'});	
	files.push({file: '201609',start: '9-1-2016',end: '9-31-2016'});	
	files.push({file: '2016s1',start: '1-1-2016',end: '3-31-2016'});	
	files.push({file: '2016s2',start: '4-1-2016',end: '6-30-2016'});	
	files.push({file: '2016s3',start: '7-1-2016',end: '9-31-2016'});	

	for( idx=0;idx<files.length;idx++ ){
		chord = new Chord();
		chord.setData(depts.groupedDept,depts.groupedDepts);
		chord.filter(files[idx].start,files[idx].end);
		chart = chord.render( dimension.values);
		chord.show();

		console.log( '===========done : '+ files[idx].file);
		fs.writeFile('data-'+files[idx].file+'.json',JSON.stringify( chart),'utf-8');
	}
});

